using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using SheLife.Application.Interfaces;
using SheLife.Application.Services;
using SheLife.Domain.Interfaces;
using SheLife.Domain.Interfaces.Repositories;
using SheLife.Infrastructure.Data;
using SheLife.Infrastructure.External;
using SheLife.Infrastructure.Repositories;

namespace SheLife.Infrastructure;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        // 注册数据库上下文
        services.AddDbContext<SheLifeDbContext>(options =>
            options.UseMySql(
                configuration.GetConnectionString("DefaultConnection"),
                ServerVersion.AutoDetect(configuration.GetConnectionString("DefaultConnection"))
            )
        );

        // 注册工作单元
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        
        // 注册日志服务
        services.AddLogging();

        // 注册应用服务
        services.AddScoped<IUserService, UserService>();
        services.AddScoped<IHealthRecordService, HealthRecordService>();
        services.AddScoped<IHabitService, HabitService>();
        services.AddScoped<IHabitRecordService, HabitRecordService>();
        services.AddScoped<IMenstrualCycleService, MenstrualCycleService>();
        services.AddScoped<IMenstrualSymptomService, MenstrualSymptomService>();
        services.AddScoped<IMenstrualNoteService, MenstrualNoteService>();
        services.AddScoped<IBasalBodyTemperatureService, BasalBodyTemperatureService>();
        services.AddScoped<IMqttPublishService, MqttPublishService>();

        // 注册外部服务
        services.AddSingleton<IRedisService>(provider => new RedisService(configuration));
        services.AddSingleton<IMinioService>(provider => new MinioService(configuration));
        services.AddSingleton<IElasticsearchService>(provider => new ElasticsearchService(configuration));
        services.AddSingleton<IMqttService>(provider => new MqttService(configuration));

        return services;
    }
}